﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using QuanLyVanBanBUS;

public partial class QLVB_User_DuyetSoanThao : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == false)
        {
            layDanhSachVanBanChuaDuyetTheoMaPhong();
        }
    }
    protected void layDanhSachVanBanChuaDuyetTheoMaPhong()
    {
        PhongBUS pBUS = new PhongBUS();
        string maNguoiDung = (String)Session["MaNguoiDung"];
        string maPhong = pBUS.layMaPhongTheoMaNguoiDung(maNguoiDung);
        
        VanBanBUS vbBUS = new VanBanBUS();
        gvDuyetSoanThao.DataSource = vbBUS.layDanhSachVanBanChuaDuyetTheoMaPhong(maPhong);
        gvDuyetSoanThao.DataBind();
    }
    protected void gvDuyetSoanThao_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        /*PhongBUS pBUS = new PhongBUS();
        string maNguoiDung = (String)Session["MaNguoiDung"];
        string maPhong = pBUS.layMaPhongTheoMaNguoiDung(maNguoiDung);
        
        VanBanBUS vbBUS = new VanBanBUS();
        gvDuyetSoanThao.PageIndex = e.NewPageIndex;
        gvDuyetSoanThao.DataSource = vbBUS.layDanhSachVanBanChuaDuyetTheoMaPhong(maPhong);
        gvDuyetSoanThao.DataBind();*/

        gvDuyetSoanThao.PageIndex = e.NewPageIndex;
        layDanhSachVanBanChuaDuyetTheoMaPhong();
    }
    protected void chkChonTatCa_CheckedChanged(object sender, EventArgs e)
    {
        MyCheckBox myCheckBox = new MyCheckBox();
        
        if (chkChonTatCa.Checked)
        {
            myCheckBox.chonTatCa(gvDuyetSoanThao, "chkMaVanBan", chkChonTatCa);
        }
        else
        {
            myCheckBox.huyTatCa(gvDuyetSoanThao, "chkMaVanBan", chkChonTatCa);
        }
    }
    protected void btnDuyet_Click(object sender, EventArgs e)
    {
        VanBanBUS vbBUS = new VanBanBUS();
        string maNguoiKiemSoat = (String)Session["MaNguoiDung"];

        NguoiDungBUS ndBUS = new NguoiDungBUS();
        string maLoaiNguoiDung = ndBUS.layMaLoaiNguoiDungTheoMaNguoiDung(maNguoiKiemSoat);
        string maPhamVi = string.Empty;
        if (maLoaiNguoiDung == "KSCN")
        {
            maPhamVi = "CN";
        }
        if (maLoaiNguoiDung == "KSPT")
        {
            maPhamVi = "PT";
        }

        //Duyet soan thao
        vbBUS.duyetSoanThao(gvDuyetSoanThao, maNguoiKiemSoat, maPhamVi);

        //Lay  danh sach gan len gvDuyetSoanThao
        layDanhSachVanBanChuaDuyetTheoMaPhong();
    }
    protected void gvDuyetSoanThao_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        /*Label lblNoiNhan = (Label)(e.Row.FindControl("lblNoiNhan"));
        if (lblNoiNhan != null)
        {
            string sNoiNhan = string.Empty;
            string maVanBan = string.Empty;
            if (e.Row.DataItemIndex >= 0)
            {
                maVanBan = gvDuyetSoanThao.DataKeys[e.Row.DataItemIndex].Value.ToString();
            }
            List<NoiNhan> lstNoiNhan = (new NoiNhanBUS()).layDanhSachMaNguoiNhanTheoMaVanBan(maVanBan);
            foreach (NoiNhan nn in lstNoiNhan)
            {
                sNoiNhan += nn.MaNguoiNhan.ToString() + "\n"; //Xuong dong
                //sNoiNhan += nn.MaNguoiNhan.ToString() + ",";    //Them dau phay ","
            }
            lblNoiNhan.Text = sNoiNhan;*/

        Label lblNoiNhan = (Label)(e.Row.FindControl("lblNoiNhan"));
        if (lblNoiNhan != null)
        {
            string sNoiNhan = string.Empty;
            string maVanBan = string.Empty;
            maVanBan = gvDuyetSoanThao.DataKeys[e.Row.RowIndex].Value.ToString();
            List<NoiNhan> lstNoiNhan = (new NoiNhanBUS()).layDanhSachMaNguoiNhanTheoMaVanBanChuaDuyet(maVanBan);
            foreach (NoiNhan nn in lstNoiNhan)
            {
                sNoiNhan += nn.MaNguoiNhan.ToString() + "\n"; //Xuong dong
                //sNoiNhan += nn.MaNguoiNhan.ToString() + ",";    //Them dau phay ","
            }
            lblNoiNhan.Text = sNoiNhan;
        }
    }
    protected void btnTuChoi_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/pHome.aspx");
    }
}